﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Jy123.Common;
using System.Collections.Generic;

namespace Jy123.Web.Module.Venues
{
    public class SaveAnnexe : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strUserId = Cookie.getCookie("UserId");
            if (!string.IsNullOrEmpty(strUserId))
            {
                int userId = int.Parse(strUserId);
                string strMId = context.Request["ModuleId"];
                if (!string.IsNullOrEmpty(strMId))
                {
                    BLL.Annexe_Record BLLRecord = new Jy123.BLL.Annexe_Record();
                    string strWhere = string.Format("FModule = {0}", strMId);
                    List<Model.Annexe_Record> listRecord = BLLRecord.GetModelList(strWhere);
                    Dictionary<int, Model.Annexe_Record> dicRecord = new Dictionary<int, Jy123.Model.Annexe_Record>();
                    foreach (Model.Annexe_Record cRecord in listRecord)
                    {
                        if (cRecord.AFather.HasValue)
                        {
                            dicRecord.Add(cRecord.AFather.Value, cRecord);
                        }
                    }
                    string[] strAnnexe = context.Request["Annexe"].Split(',');
                    //保存设施的使用记录
                    foreach (Model.Annexe_Record record in listRecord)
                    {
                        if (!record.AFather.HasValue)
                        {
                            record.IsUsed = IsInList(record, strAnnexe);
                            BLLRecord.Update(record);
                        }
                        else
                        {
                            if (!IsInList(record, strAnnexe))
                            {
                                //删除不再使用的广告记录
                                BLLRecord.Delete(record.ID);
                            }
                        }
                    }
                    //保存仍然使用或新增的广告记录
                    if (strAnnexe.Length > 0)
                    {
                        for (int i = 0; i < strAnnexe.Length; i++)
                        {
                            string strId = strAnnexe[i];
                            if (strId.Contains("|"))
                            {
                                string[] strCurrent = strId.Split('|');
                                int cId = int.Parse(strCurrent[0]);
                                int aId = int.Parse(strCurrent[1]);
                                if (IsInModelList(cId, listRecord))
                                {
                                    Model.Annexe_Record currentRecord = dicRecord[cId];
                                    currentRecord.FAnnexe = aId;
                                    BLLRecord.Update(currentRecord);
                                }
                                else
                                {
                                    if (cId > 0 && aId > 0)
                                    {
                                        Model.Annexe_Record newRecord = new Jy123.Model.Annexe_Record();
                                        newRecord.AFather = cId;
                                        newRecord.FAnnexe = aId;
                                        newRecord.FModule = int.Parse(strMId);
                                        newRecord.IsUsed = true;
                                        BLLRecord.Add(newRecord);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                context.Response.Redirect("/");
            }
        }

        private bool IsInList(Model.Annexe_Record record, string[] strAnnexe)
        {
            if (strAnnexe.Length > 0)
            {
                for (int i = 0; i < strAnnexe.Length; i++)
                {
                    string strId = strAnnexe[i];
                    if (!string.IsNullOrEmpty(strId))
                    {
                        int cId = 0;
                        if (strId.Contains("|"))
                        {
                            string[] strCurrent = strId.Split('|');
                            cId = int.Parse(strCurrent[0]);
                        }
                        else
                        {
                            cId = int.Parse(strId);
                        }

                        if (record.FAnnexe == cId)
                        {
                            return true;
                        }
                        else
                        {
                            if (record.AFather.HasValue)
                            {
                                return record.AFather.Value == cId;
                            }
                        }
                    }
                }
            }
            return false;
        }

        private bool IsInModelList(int id, List<Model.Annexe_Record> listRecord)
        {
            foreach (Model.Annexe_Record record in listRecord)
            {
                if (record.AFather.HasValue)
                {
                    return record.AFather.Value == id;
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
